using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;
namespace Impact.Model.Objects
{
	/// <summary>
	/// Strongly-typed collection for the PhotoGallery class.
	/// </summary>
    [Serializable]
	public partial class PhotoGalleryCollection : ActiveList<PhotoGallery, PhotoGalleryCollection>
	{	   
		public PhotoGalleryCollection() {}
	}
	/// <summary>
	/// This is an ActiveRecord class which wraps the impact_PhotoGalleries table.
	/// </summary>
	[Serializable]
	public partial class PhotoGallery : ActiveRecord<PhotoGallery>, IActiveRecord
	{
		#region .ctors and Default Settings
		
		public PhotoGallery()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}
		
		private void InitSetDefaults() { SetDefaults(); }
		
		public PhotoGallery(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}
        
		public PhotoGallery(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}
		 
		public PhotoGallery(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}
		
		protected static void SetSQLProps() { GetTableSchema(); }
		
		#endregion
		
		#region Schema and Query Accessor	
		public static Query CreateQuery() { return new Query(Schema); }
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}
		}
		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("impact_PhotoGalleries", TableType.Table, DataService.GetInstance("ImpactProvider"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarGalleryID = new TableSchema.TableColumn(schema);
				colvarGalleryID.ColumnName = "GalleryID";
				colvarGalleryID.DataType = DbType.Int32;
				colvarGalleryID.MaxLength = 0;
				colvarGalleryID.AutoIncrement = true;
				colvarGalleryID.IsNullable = false;
				colvarGalleryID.IsPrimaryKey = true;
				colvarGalleryID.IsForeignKey = false;
				colvarGalleryID.IsReadOnly = false;
				colvarGalleryID.DefaultSetting = @"";
				colvarGalleryID.ForeignKeyTableName = "";
				schema.Columns.Add(colvarGalleryID);
				
				TableSchema.TableColumn colvarName = new TableSchema.TableColumn(schema);
				colvarName.ColumnName = "Name";
				colvarName.DataType = DbType.AnsiString;
				colvarName.MaxLength = 50;
				colvarName.AutoIncrement = false;
				colvarName.IsNullable = false;
				colvarName.IsPrimaryKey = false;
				colvarName.IsForeignKey = false;
				colvarName.IsReadOnly = false;
				colvarName.DefaultSetting = @"";
				colvarName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarName);
				
				TableSchema.TableColumn colvarDescription = new TableSchema.TableColumn(schema);
				colvarDescription.ColumnName = "Description";
				colvarDescription.DataType = DbType.AnsiString;
				colvarDescription.MaxLength = 2147483647;
				colvarDescription.AutoIncrement = false;
				colvarDescription.IsNullable = true;
				colvarDescription.IsPrimaryKey = false;
				colvarDescription.IsForeignKey = false;
				colvarDescription.IsReadOnly = false;
				colvarDescription.DefaultSetting = @"";
				colvarDescription.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDescription);
				
				TableSchema.TableColumn colvarCreatedOn = new TableSchema.TableColumn(schema);
				colvarCreatedOn.ColumnName = "CreatedOn";
				colvarCreatedOn.DataType = DbType.DateTime;
				colvarCreatedOn.MaxLength = 0;
				colvarCreatedOn.AutoIncrement = false;
				colvarCreatedOn.IsNullable = false;
				colvarCreatedOn.IsPrimaryKey = false;
				colvarCreatedOn.IsForeignKey = false;
				colvarCreatedOn.IsReadOnly = false;
				colvarCreatedOn.DefaultSetting = @"";
				colvarCreatedOn.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCreatedOn);
				
				TableSchema.TableColumn colvarCreatedBy = new TableSchema.TableColumn(schema);
				colvarCreatedBy.ColumnName = "CreatedBy";
				colvarCreatedBy.DataType = DbType.String;
				colvarCreatedBy.MaxLength = 50;
				colvarCreatedBy.AutoIncrement = false;
				colvarCreatedBy.IsNullable = false;
				colvarCreatedBy.IsPrimaryKey = false;
				colvarCreatedBy.IsForeignKey = false;
				colvarCreatedBy.IsReadOnly = false;
				colvarCreatedBy.DefaultSetting = @"";
				colvarCreatedBy.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCreatedBy);
				
				TableSchema.TableColumn colvarVisible = new TableSchema.TableColumn(schema);
				colvarVisible.ColumnName = "Visible";
				colvarVisible.DataType = DbType.Boolean;
				colvarVisible.MaxLength = 0;
				colvarVisible.AutoIncrement = false;
				colvarVisible.IsNullable = false;
				colvarVisible.IsPrimaryKey = false;
				colvarVisible.IsForeignKey = false;
				colvarVisible.IsReadOnly = false;
				
						colvarVisible.DefaultSetting = @"((1))";
				colvarVisible.ForeignKeyTableName = "";
				schema.Columns.Add(colvarVisible);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["ImpactProvider"].AddSchema("impact_PhotoGalleries",schema);
			}
		}
		#endregion
		
		#region Props
		  
		[XmlAttribute("GalleryID")]
		[Bindable(true)]
		public int GalleryID 
		{
			get { return GetColumnValue<int>(Columns.GalleryID); }
			set { SetColumnValue(Columns.GalleryID, value); }
		}
		  
		[XmlAttribute("Name")]
		[Bindable(true)]
		public string Name 
		{
			get { return GetColumnValue<string>(Columns.Name); }
			set { SetColumnValue(Columns.Name, value); }
		}
		  
		[XmlAttribute("Description")]
		[Bindable(true)]
		public string Description 
		{
			get { return GetColumnValue<string>(Columns.Description); }
			set { SetColumnValue(Columns.Description, value); }
		}
		  
		[XmlAttribute("CreatedOn")]
		[Bindable(true)]
		public DateTime CreatedOn 
		{
			get { return GetColumnValue<DateTime>(Columns.CreatedOn); }
			set { SetColumnValue(Columns.CreatedOn, value); }
		}
		  
		[XmlAttribute("CreatedBy")]
		[Bindable(true)]
		public string CreatedBy 
		{
			get { return GetColumnValue<string>(Columns.CreatedBy); }
			set { SetColumnValue(Columns.CreatedBy, value); }
		}
		  
		[XmlAttribute("Visible")]
		[Bindable(true)]
		public bool Visible 
		{
			get { return GetColumnValue<bool>(Columns.Visible); }
			set { SetColumnValue(Columns.Visible, value); }
		}
		
		#endregion
		
		
		#region PrimaryKey Methods		
		
        protected override void SetPrimaryKey(object oValue)
        {
            base.SetPrimaryKey(oValue);
            
            SetPKValues();
        }
        
				
		private Impact.Model.Objects.PhotoCollection colPhotos;
		public Impact.Model.Objects.PhotoCollection Photos
		{
			get
			{
				if(colPhotos == null)
				{
					colPhotos = new Impact.Model.Objects.PhotoCollection().Where(Photo.Columns.GalleryID, GalleryID).Load();
					colPhotos.ListChanged += new ListChangedEventHandler(colPhotos_ListChanged);
				}
				return colPhotos;			
			}
			set 
			{ 
					colPhotos = value; 
					colPhotos.ListChanged += new ListChangedEventHandler(colPhotos_ListChanged);
			}
		}
		
		void colPhotos_ListChanged(object sender, ListChangedEventArgs e)
		{
		    if (e.ListChangedType == ListChangedType.ItemAdded)
		    {
		        // Set foreign key value
		        colPhotos[e.NewIndex].GalleryID = GalleryID;
		    }
		}
		
		#endregion
		
			
		
		//no foreign key tables defined (0)
		
		
		
		//no ManyToMany tables defined (0)
		
        
        
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(string varName,string varDescription,DateTime varCreatedOn,string varCreatedBy,bool varVisible)
		{
			PhotoGallery item = new PhotoGallery();
			
			item.Name = varName;
			
			item.Description = varDescription;
			
			item.CreatedOn = varCreatedOn;
			
			item.CreatedBy = varCreatedBy;
			
			item.Visible = varVisible;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varGalleryID,string varName,string varDescription,DateTime varCreatedOn,string varCreatedBy,bool varVisible)
		{
			PhotoGallery item = new PhotoGallery();
			
				item.GalleryID = varGalleryID;
			
				item.Name = varName;
			
				item.Description = varDescription;
			
				item.CreatedOn = varCreatedOn;
			
				item.CreatedBy = varCreatedBy;
			
				item.Visible = varVisible;
			
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		#endregion
        
        
        
        #region Typed Columns
        
        
        public static TableSchema.TableColumn GalleryIDColumn
        {
            get { return Schema.Columns[0]; }
        }
        
        
        
        public static TableSchema.TableColumn NameColumn
        {
            get { return Schema.Columns[1]; }
        }
        
        
        
        public static TableSchema.TableColumn DescriptionColumn
        {
            get { return Schema.Columns[2]; }
        }
        
        
        
        public static TableSchema.TableColumn CreatedOnColumn
        {
            get { return Schema.Columns[3]; }
        }
        
        
        
        public static TableSchema.TableColumn CreatedByColumn
        {
            get { return Schema.Columns[4]; }
        }
        
        
        
        public static TableSchema.TableColumn VisibleColumn
        {
            get { return Schema.Columns[5]; }
        }
        
        
        
        #endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string GalleryID = @"GalleryID";
			 public static string Name = @"Name";
			 public static string Description = @"Description";
			 public static string CreatedOn = @"CreatedOn";
			 public static string CreatedBy = @"CreatedBy";
			 public static string Visible = @"Visible";
						
		}
		#endregion
		
		#region Update PK Collections
		
        public void SetPKValues()
        {
                if (colPhotos != null)
                {
                    foreach (Impact.Model.Objects.Photo item in colPhotos)
                    {
                        if (item.GalleryID != GalleryID)
                        {
                            item.GalleryID = GalleryID;
                        }
                    }
               }
		}
        #endregion
    
        #region Deep Save
		
        public void DeepSave()
        {
            Save();
            
                if (colPhotos != null)
                {
                    colPhotos.SaveAll();
               }
		}
        #endregion
	}
}
